package com.baidu.cmsService.services.impls;

import com.baidu.cmsCommes.pojo.*;
import com.baidu.cmsService.dao.ChannelDao;
import com.baidu.cmsService.dao.GroupChannelDao;
import com.baidu.cmsService.dao.GroupDao;
import com.baidu.cmsService.dao.UserGroupDao;
import com.baidu.cmsService.services.GroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/6/18/15:14
 */
@Service
public class GroupServiceImpl implements GroupService {
    @Resource
    private GroupDao groupDao;

    @Resource
    private UserGroupDao userGroupDao;

    @Resource
    private GroupChannelDao groupChannelDao;

    @Resource
    private ChannelDao channelDao;

    public List<Group> groupList() {
        return groupDao.groupList();
    }

    public Pager<Group> groupListPage(Integer offset,Integer size) {
        Integer count = groupDao.selectCount();
        Integer index = (offset-1)*size;
        List<Group> groupList = groupDao.selectListPage(index,size);

        Pager<Group> groupPager = new Pager<Group>(size,offset,count,groupList);

        return groupPager;
    }

    /**
     * 未能返回主键
     * @param group
     * @return
     */
    public Boolean saveGroup(Group group) {
        Integer id = groupDao.saveGroup(group);
        return id!=null?true:false;
    }

    public Boolean deleteGroupById(Integer gid) {
        Integer sccess = groupDao.deleteGroupById(gid);
        return sccess==null?false:true;
    }

    public Group selectById(Integer gid) {
        return groupDao.selectById(gid);
    }

    public Boolean updateGroupById(Group group) {
        return groupDao.updateGroupById(group)==1?true:false;
    }

    public Boolean clearUsers(Integer gid) {
        Integer deleteSuccess = userGroupDao.deleteByGid(gid);
        return deleteSuccess==null?false:true;
    }

    public List<Channel> showTree(Integer gid) {
        //根据gid查询出该组下的所有栏目id
        List<GroupChannel> groupChannels = groupChannelDao.selectByGid(gid);

        //根据栏目id查询栏目
        List<Channel> list = new ArrayList<Channel>();
        for (GroupChannel groupChannel : groupChannels) {
            list.add(channelDao.selectById(groupChannel.getC_id()));
        }

        return list;
    }

    public List<Integer> getCids(Integer gid) {
        List<Integer> cids = new ArrayList<Integer>();

        List<GroupChannel> groupChannels = groupChannelDao.selectByGid(gid);
        for (GroupChannel groupChannel : groupChannels) {
            cids.add(groupChannel.getC_id());
        }

        return cids;
    }

    public List<Channel> treeAll() {
        List<Channel> channelList = channelDao.listTree();
        return channelList;
    }


}
